import os
import cv2
import time
import shutil
from tqdm import tqdm
import numpy as np


SrcDir = r"/media/fang/TOSHIBA EXT/Temp/Temp10/after"
DstDir = r"/media/fang/TOSHIBA EXT/ImgOutput/BackUp/Circle/Limit/Append/0311"

RepeatCount = 1

for root, dirs, files in tqdm(os.walk(SrcDir), position=1):
    if len(files) != 0:
        print("\n%s moving......" % root)
        time.sleep(0.1)
    for file in tqdm(files, position=0):
        if file[-1] == 'l':
            if os.path.exists(DstDir + "/" + file) is False:
                shutil.copyfile(root + "/" + file, DstDir + "/" + file)
                shutil.copyfile(root + "/" + file[:-3] + "jpg", DstDir + "/" + file[:-3] + "jpg")
            else:
                Img1 = cv2.imread(DstDir + "/" + file[:-3] + "jpg")
                Img2 = cv2.imread(root + "/" + file[:-3] + "jpg")
                try:
                    difference = np.any(cv2.subtract(Img1, Img2))
                except:
                    difference = True
                if difference:
                    shutil.copyfile(root + "/" + file, DstDir + "/Repeat%05d" % RepeatCount + file)
                    shutil.copyfile(root + "/" + file[:-3] + "jpg", DstDir + "/Repeat%05d" % RepeatCount + file[:-3] + "jpg")
                    RepeatCount += 1


# RenameJpgAndXml(DstDir)
print("--------------------------------done---------------------------")
